package com.ocom.xfrecode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityNotMealRecode;
import com.ocom.common.entity.card.EntityXfRecord;
import com.ocom.common.request.xfrecde.NotMealRecodeRequest;
import com.ocom.xfrecode.mapper.NotMealRecodeMapper;
import com.ocom.xfrecode.service.NotMealRecodeService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class NotMealRecodeServiceImpl extends ServiceImpl<NotMealRecodeMapper, EntityNotMealRecode> implements NotMealRecodeService {
    @Override
    public boolean save(EntityNotMealRecode entity) {
        //查询如果存在
        QueryWrapper<EntityNotMealRecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("month_value",entity.getMonthValue())
                .eq("p_id",entity.getPid())
                .eq("meal_date",entity.getMealDate())
                .eq("dept_id",entity.getDeptId())
                .eq("com_id",entity.getComId())
                .eq("meal_no",entity.getMealNo())
                .last("limit 1")
        ;
        EntityNotMealRecode mealRecode = this.getOne(queryWrapper);
        entity.setCreateTime(new Date());
        if(ObjectUtil.isEmpty(mealRecode)){
            return super.save(entity);
        }
        return true;
    }

    @Override
    public Page<EntityNotMealRecode> getNotMealRecode(NotMealRecodeRequest request) {
        String startMonth = ObjectUtil.isEmpty(request.getStartTime())?DateUtil.format(new Date(),"yyyyMM"):DateUtil.format(request.getStartTime(),"yyyyMM");
        String endMonth = ObjectUtil.isEmpty(request.getStartTime())?DateUtil.format(new Date(),"yyyyMM"):DateUtil.format(request.getEndTime(),"yyyyMM");
        Page<EntityNotMealRecode> page = new Page<>(request.getPage(), request.getSize());
        QueryWrapper<EntityNotMealRecode> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(ObjectUtil.isNotEmpty(request.getPName()),"p_name",request.getPName())
                .eq(ObjectUtil.isNotEmpty(request.getPCode()),"p_code",request.getPCode())
                .eq("com_id",request.getComId())
                .eq(ObjectUtil.isNotEmpty(request.getMealNo()),"meal_no",request.getMealNo())
                .in(ObjectUtil.isNotEmpty(request.getDeptList()),"dept_id",request.getDeptList())
                .and(field->{
                    field.eq("month_value",startMonth)
                        .or().eq("month_value",endMonth);
                })
                .between(ObjectUtil.isNotEmpty(request.getStartTime()) && ObjectUtil.isNotEmpty(request.getEndTime()),"meal_date",request.getStartTime(),request.getEndTime())
                .orderByDesc("meal_date","create_time")
            ;
        return this.page(page,queryWrapper);
    }
}
